<?php

require_once dirname(__FILE__).'/../../query/lib/SimpleContactForm.class.php';
require_once dirname(__FILE__).'/../../query/lib/queryHelper.class.php';
require_once dirname(__FILE__).'/../../query/lib/queryConfiguration.class.php';


class contactUsFormComponents extends myComponents
{
  public function executeRender()
  {
      $this->title         = $this->getRoute()->getObject()->getPageTitle();
      $this->contents      = $this->getRoute()->getObject()->getContent();
      $this->configuration = new queryConfiguration();
      $this->form          = $this->configuration->getSimpleContactForm();
      $this->request       = sfContext::getInstance()->getRequest();
      
      if($this->request->isMethod('post'))
      {
          $this->processForm();
      }
  }
  
  private function processForm()
  {
      $this->form->bind($this->request->getParameter($this->form->getName()));
      
      if($this->form->isValid())
      {
          $this->sendEmail(
            variable('email', 'email@example.com'),
            variable('email', 'email@example.com'),
            variable('email', 'email@example.com'),
            'Contact form', 
            implode('<br />',$this->form->getValues())
          );
          
          $this->getUser()->setFlash('errors', 'Edukalt saadetud');
          $this->getController()->redirect(LinkGen::getInstance(LinkGen::STRUCTURE)->link($this->getRoute()->getObject()->getId()));
          
          return sfView::NONE;
      }
      else
      {
          $this->getUser()->setFlash('errors', implode('<br />', $this->form->getErrors()));
      }
  }
  
  private function sendEmail($from, $fromName, $to, $subject, $content)
  {
      $message = $this->getMailer()
          ->compose(
                array( $from => $fromName ),
		      	$to,
		      	$subject,
		      	$content 
		   );
       $message->setContentType('text/html'); 
	   $this->getMailer()->send( $message );
  }
};